//拼接链接函数
function url( rule ) {
    if ( RULE_TYPE == 'pathInfo' ) {
        var url = HOST + rule;
    } else if ( RULE_TYPE == 'get' ){
        var url = HOST + "?r=" + rule;
    }
    return url;
}




//表单本地验证和提交验证
function formVerification( form, rulesSetting, callback ) {
    $(form).submit(function () {
        $(".form-error-message").each(function() {
            this.remove();
        });
        var isError = false;
        var postValue = {};
        var handleError = function( elemId, errorMessage) {
            $("#" + elemId).after("<p class='form-error-message' style='color:#DE5C5B'>"+errorMessage+"</p>");
        };
       
        $.each( rulesSetting, function() {
            var id = this.id;
            var required = this.required;
            var value = $("#" + id ).val();
            var rules = this.rules;
            var message = this.errormessage;

//             if ( (required == true) && (value ==='') ) {
//                handleError(id, this.errormessage);
//                isError = true;
//                return true;
//            }
//
//            if ( (value !=='') && ( rules !== undefined )){
//                $.each(rules, function() {
//                    if ( this.rule(value) == false ) {
//                        handleError(id,this.error);
//                        isError = true;
//                        return false;
//                    }
//                });
//            }

            if ( isError == false ) {
                postValue[ id ] = value;
            }

        });
        
        if ( isError == true ) {
            return false;
        }
        
        var btn = $("button[type='submit']", form).button('loading');
        $.post( $(form).attr('action'), postValue, function( result ) {
            btn.button('reset');
            var ajError = false;
            if ( result.checkWork === undefined ) {
                if ( result.otherError === undefined ) {
                    $.each( result, function () {
                        if (this.result === false) {
                            ajError = true;
                            handleError(this.id, this.message);
                        }
                    });
                } else {
                    $('body').append("<div class='alert alert-warning' style='margin-top:15px;padding: 70px 20px;position: absolute;width: 350px;color: #4C4A4A;background-color: #F1F1F1; border-color: #9E9E9E;    left: 45%; top: 70%;'><p>" + result.otherError + "</p><button type='button' class='close' data-dismiss='alert' style='padding: 0px 15px;border: 1px solid #B9B9B9;position: absolute;bottom: 5px;right: 10px;background-color: #D4D4D4; opacity: 1;color: #616161;'>&times;</button></div>");
                    ajError = true;
                }
            }
            
            if ( ajError != true ) {
                callback();
            }
        }, 'json');
        
        return false;
        
    });
}


//擦除选中列表行
function deleteList( id, warningMesg, handleUrl, goUrl ) {
    $(id).bind("click", function() {
        
        var value = new Array();
        $("input[class='select-box']").each( function() {
           if ( this.checked == true ) {
               value.push( this.value); 
           } 
        });
        
        if ( value.length > 0 ) {
            var warning = confirm( warningMesg );
        } else {
            return false;
        }
            
        if( warning == true ) {
            value = value.join(",");
            var btn = $( id ).button('loading');
            $.post( handleUrl, { 'value':value }, function(data) {
                btn.button('reset');
                if ( data.delresult == true ) {
                    location = goUrl;
                } else if ( data.delresult == false ) {
                    $('body').append("<div class='alert alert-warning' style='position:absolute; padding: 50px;opacity: 1;'>数据读写异常，请稍后再试！<button type='button' class='close' data-dismiss='alert' style='padding: 0px 15px;position: absolut; right: 20px;top: 90px;border: 1px solid #E2DDDD;background-color: #EFEAEA; opacity: 1;color: #8A6D3B;'>&times;</button></div>");
                    var left = ($( window ).width() - $( ".alert " ).width()) / 2;
                    var top = ($( window ).height() - $( ".alert " ).height()) / 2;
                    $('.alert').css({left:left, top: top });
                }
            }, 'json');

        } else {
            return false;
        }
    });
}

//发布或编辑文章的提交按钮点击函数
function artissue( id, handleurl ) {
    $( id ).click( function() {
        editor.sync();
        var sortval = new Array();
        $("input[class='artwrite-sortchoice-box']").each( function() {
           if ( this.checked == true ) {
               sortval.push( this.value); 
           } 
        }); 
        sortval = sortval.join(",");
        var data = {
            art: $('#editor_id').val(),
            arttitle: $('#art_title').val(),
            sortval: sortval,
            artid: $('#artid').val(),
            date: $('.datepicker').val(),
            outline: $("#outline").val()
        };

        var btn = $(id).button('loading');
        $.post( handleurl, data, function( result ) {
            btn.button('reset');
            if ( result.issueResult == true ) {
                location = url('admin/artlist');
            } else {
                $('body').append("<div class='alert alert-warning' style='margin-top:15px;padding: 70px 20px;position: absolute;width: 350px;color: #4C4A4A;background-color: #F1F1F1; border-color: #9E9E9E;    left: 45%; top: 70%;'><p>" + result.error + "</p><button type='button' class='close' data-dismiss='alert' style='padding: 0px 15px;border: 1px solid #B9B9B9;position: absolute;bottom: 5px;right: 10px;background-color: #D4D4D4; opacity: 1;color: #616161;'>&times;</button></div>");
            }
        },'json');
        return false;
    });
}